package com.inspinia.config.common.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ResourceBundle;

/**
 * 系统参数设置
 *
 * @author veblen
 * @date 2017/1/22
 */
@Slf4j
@Data
public class SystemConfig {
    private static ResourceBundle SYS_CFG_BUNDLE = ResourceBundle.getBundle("system");

    private static String getString(String key) {
        return SYS_CFG_BUNDLE.getString(key);
    }
    /**
     * 登录页面标题
     */
    public static String systemLoginPageTitle = getString("login.page.title");
    /**
     * 登录面板标题
     */
    public static String systemLoginPanelTitle = getString("login.panel.title");
    /**
     * 是否有保存密码功能
     */
    public static String systemLoginFindPassword = null;
    /**
     * 系统页面下标
     */
    public static String systemPagefoot = getString("company.name");
    /**
     * 主页页面标题
     */
    public static String systemIndexPageTitle = getString("index.page.title");

    /**
     * 主页系统名称
     */
    public static String systemIndexTitleName = getString("index.title.name");

    /**
     * 系统版本
     */
    public static String version = getString("version");

    /**
     * 系统默认密码
     */
    public static String defaultPassword = getString("default.password");


    /**
     *是否要显示logo
     */
    public static String isShowLogo = getString("show_logo");

    /**
     * ip
     */
    public static String ip = getString("ip");
    /**
     *端口
     */
    public static int port = Integer.valueOf(getString("port"));

    /**
     *  监听配置
     * @param changeEvent
     */
    @ApolloConfigChangeListener("system")
    public void systemChange(ConfigChangeEvent changeEvent){
        log.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType());
            if ("login.page.title".equals(change.getPropertyName())) {
                systemLoginPageTitle = change.getNewValue();
            }
            if ("login.panel.title".equals(change.getPropertyName())) {
                systemLoginPanelTitle = change.getNewValue();
            }
            if ("company.name".equals(change.getPropertyName())) {
                systemPagefoot = change.getNewValue();
            }
            if ("index.page.title".equals(change.getPropertyName())) {
                systemIndexPageTitle = change.getNewValue();
            }
            if ("index.title.name".equals(change.getPropertyName())) {
                systemIndexTitleName = change.getNewValue();
            }
            if ("version".equals(change.getPropertyName())) {
                version = change.getNewValue();
            }
            if ("defaultPassword".equals(change.getPropertyName())) {
                defaultPassword = change.getNewValue();
            }
            if ("show_logo".equals(change.getPropertyName())) {
                isShowLogo = change.getNewValue();
            }
        }
    }

}

